package com.example.project_01.controller;

import com.example.project_01.commons.Result;
import com.example.project_01.model.CityModel;
import com.example.project_01.service.CityService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController    //标记控制器
@RequestMapping("/city")   //定义请求映射路径
@CrossOrigin   //跨域请求
//@Api(tags = "城市信息控制器")   //API文档生成和分组
@Tag(name = "城市信息接口", description = "这个接口是城市信息的接口")
public class CityController {
    @Autowired
    CityService cityService;

    //分页查询所有城市信息，name不为空则模糊查询
    @GetMapping(value = {"/getCity/{pn}/{size}", ""})
    @Operation(summary = "城市信息",description = "分页查询城市信息")
    public Result getCityWithPage(@PathVariable(value = "pn",required = false) Integer pn,
                                  @PathVariable(value = "size",required = false) Integer size,
                                  @Parameter(
                                          name = "name",
                                          description = "城市名称，支持模糊查询，为空时查询所有城市",
                                          example = "北京",
                                          required = false,
                                          schema = @Schema(type = "string")
                                  )
                                  @RequestParam(value = "name", required = false) String name){
        PageInfo<CityModel> info = cityService.getCityWithPage(pn, size, name);
        if (info != null) {
            return new Result(200,"城市信息响应成功",info);
        }
        return new Result(201,"城市信息响应失败",null);
    }

    //根据id查询一个城市信息
    @GetMapping("/selectCityById/{id}")
    @Operation(summary = "根据城市id查询城市信息",description = "根据城市id查询城市信息")
    public Result selectCityById(@PathVariable("id") Integer id){
        return cityService.getCityById(id);
    }

    //添加城市
    //@RolesAllowed({"1"})    //允许访问的角色
    @PostMapping( "/addCity")
    @Operation(summary = "输入城市编号添加城市信息",description = "输入城市编号添加城市信息")
    public Result addCity(@RequestBody Integer cityNumber){
        if (cityService.checkCityByNum(cityNumber) > 0){
            return new Result(201,"城市已存在",null);
        }
        return cityService.addCity(cityNumber);
    }

    //根据id删除城市信息
    @DeleteMapping("/deleteCityById/{id1}")
    @Operation(summary = "根据城市id删除城市",description = "根据城市id删除城市")
    public Result deleteCityById(@PathVariable("id1") Integer id){
        return cityService.deleteCityById(id);
    }





}
